<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'AddProduct.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <script type="text/javascript"
	src="/EasyBuy/statics/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/menu.js"></script>

<script type="text/javascript" src="js/select.js"></script>
</head>

<body>
	<%@ include file="../../common/header.jsp" %>
	<%@ include file="../../common/backendHeader.jsp"%>
	
	<c:choose>
		<c:when test="${empty applicationScope.type }">
			<% 	request.getRequestDispatcher("../../UserServlet?opr=type&href=AddProduct&href1=product").forward(request, response); %>
		</c:when>
		<c:otherwise>
			<c:if test="${applicationScope.type==1 }">
  				<%@ include file="../../common/sideMamber.jsp" %> 
			</c:if>
			<c:if test="${applicationScope.type==0 }">
				<%@ include file="../../common/sideUser.jsp" %> 
			</c:if>
		</c:otherwise>
	 </c:choose>
	
		<div class="m_right">
			<p></p>
			<div class="mem_tit">商品上架</div>
			<form id="form">
				<table border="0" class="order_tab"
					style="width:930px; text-align:center; margin-bottom:30px;"
					cellspacing="0" cellpadding="0">
					<tr>
						<td width="25%" height="50px" align="right">一级分类</td>
						<td align="left"><select name="cosmetics" id="cosmetics"
							style="width:200px">
								
						</select></td>
					</tr>

					<tr>
						<td width="25%" height="50px" align="right">二级分类</td>
						<td align="left"><select name="face" id="face"
							style="width:200px">
								
						</select></td>
					</tr>

					<tr>
						<td width="25%" height="50px" align="right">三级分类</td>
						<td align="left"><select name="girl" id="girl"
							style="width:200px">
								
						</select></td>
					</tr>
					<tr>
						<td width="25%" height="50px" align="right">所属品牌</td>
						<td align="left"><select name="brand" id="brand"
							style="width:200px">
								
						</select></td>
					</tr>
					<tr>
						<td width="25%" height="50px" align="right">商品名称</td>
						<td align="left"><input type="text" name="productname"
							id="productname" style="width:200px" value="">(必填)</td>
					</tr>

					<tr>
						<td width="25%" height="50px" align="right">商品图片</td>
						<td align="left"><img
							src="/EasyBuy/statics/images/bk_3.jpg" id="img" height="100px" width="100px"> <input
							type="file" name="pic" id="pic" accept="image/*" style="width:200px"
							value=""></td>
					</tr>

					<tr>
						<td width="25%" height="50px" align="right">单价</td>
						<td align="left"><input type="text" name="price" id="price"
							style="width:200px" value="">
						</td>

					</tr>

					<tr>
						<td width="25%" height="50px" align="right">库存</td>
						<td align="left"><input type="text" name="kusave" id="kusave"
							style="width:200px" value="">
						</td>
					</tr>

					<tr>
						<td width="25%" height="50px" align="right">描述</td>
						<td align="left"><input type="text" name="desc" id="desc"
							style="width:200px" value="">
						</td>
					</tr>

					<tr>
						<td width="25%" height="50px"></td>
						<td align="left"><input type="hidden"
							value="" name="id" id="id" /> <input name="xiugai"
							type="button" id="bto" value="确定上架 " onClick="search();"
							class="btn2" style="width:200px" /></td>
					</tr>
				</table>
			</form>
		</div>
	<%@ include file="../../common/footer.jsp"%>
	<script type="text/javascript"
		src="/EasyBuy/statics/js/jquery-1.4.2.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function() {
			//上传文件
			//头像上传预览
		    $("#pic").change(function() {
		        var $file = $(this);
		        var fileObj = $file[0];
		        var windowURL = window.URL || window.webkitURL;
		        var dataURL;
		        var $img = $("#img");
		        if(fileObj && fileObj.files && fileObj.files[0]){
		        dataURL = windowURL.createObjectURL(fileObj.files[0]);
		        $img.attr('src',dataURL);
		        }else{ 
		        dataURL = $file.val();
		        var imgObj = document.getElementById("ImgPr");
		        imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
		        imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;
		         
		        }
		    });
		
			$("#bto").click(function() {
				if ($("#productname").val() == "" || $("#productname").val() == null) {
						alert("商品名称不能为空");
				} else if($("#price").val() == "" || $("#price").val() == null) {
						alert("单价不能为空");
				} else if($("#kusave").val() == "" || $("#kusave").val() == null) {
						alert("库存不能为空");
				} else if($("#desc").val() == "" || $("#desc").val() == null) {
						alert("描述不能为空");
				} else if($("#cosmetics").val() == "0" ){
					alert("请选择一级分类");
				} else {
					//var id=$("#id").val();
					var productname=$("#productname").val();
					var price=$("#price").val();
					var kusave=$("#kusave").val();
					var desc=$("#desc").val();
					var girl=$("#girl").val();
					var face=$("#face").val();
					var cosmetics=$("#cosmetics").val();
					var pic=$("#pic").val();
					var reg=/[\\\/]/g;
					var pic1=pic.replace(reg,"-");
					var brandId=$("#brand").val();
					var str="&productname="+productname+"&price="+price+"&kusave="+kusave+"&desc="+desc+"&girl="+girl+"&face="+face+"&cosmetics="+cosmetics+"&brandId="+brandId+"&pic1="+pic1;
					$.getJSON("CommodityInformationServlet","opr=productAdd"+str,callBackAdd);
					alert(str);
					function callBackAdd(data){
						if(data==1){
							alert("添加成功！");
							location.href="backend/product/ProductList.jsp";
						}else{
							alert("添加失败！");
							location.href="backend/product/ProductList.jsp";
						}
					}
				}
			});
			$("#brand").append("<option value='0' selected='selected'>请选择</option>");
			$.getJSON("CommodityInformationServlet","opr=brandName",callBackBrand);
			function callBackBrand(dataBrand){
				$("#brand").html("");
				$("#brand").append("<option value='0' selected='selected'>请选择</option>");
				for(var i=0;i<dataBrand.length;i++){
					$("#brand").append("<option value='"+dataBrand[i].brand_id+"'>"+dataBrand[i].brand_name+"</option>");
				}
			}
			$("#cosmetics").append("<option class='oneQing' value='0' selected='selected'>请选择</option>");
			$.getJSON("CommodityInformationServlet","opr=categoryOneName",callBackOne);
			function callBackOne(dataOne){
				$("#cosmetics").html("");
				$("#cosmetics").append("<option class='oneQing' value='0' selected='selected'>请选择</option>");
				for(var i=0;i<dataOne.length;i++){
					$("#cosmetics").append("<option value='"+dataOne[i].id+"'>"+dataOne[i].name+"</option>");
				}
			}
			$("#face").append("<option value='0' selected='selected'>请选择</option>");
			$("#cosmetics").change(function(){
				var id=$(this).val();
				$.getJSON("CommodityInformationServlet","opr=categoryTwoNames&id="+id,callBackTwo);
				function callBackTwo(dataTwo){
					$("#face").html("");
					$("#face").append("<option value='0' selected='selected'>请选择</option>");
					for(var i=0;i<dataTwo.length;i++){
						$("#face").append("<option value='"+dataTwo[i].id+"'>"+dataTwo[i].name+"</option>");
					}
				}
			});
			$("#girl").append("<option value='0' selected='selected'>请选择</option>");
			$("#face").change(function(){
				var id=$(this).val();
				$.getJSON("CommodityInformationServlet","opr=categoryThreeNames&id="+id,callBackThree);
				function callBackThree(dataThree){
					$("#girl").html("");
					$("#girl").append("<option value='0' selected='selected'>请选择</option>");
					for(var i=0;i<dataThree.length;i++){
						$("#girl").append("<option value='"+dataThree[i].id+"'>"+dataThree[i].name+"</option>");
					}
				}
			});
		});
	</script>
</body>
</html>
